package com.blue.alipay.utils.property;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * 资源文件工具类
 * 作者：ming.guo
 * 时间：2016/8/23 17:47
 */
public class PropertiesUtil extends PropertyPlaceholderConfigurer {
	private static Logger logger = LogManager.getLogger(PropertiesUtil.class);
    private static Map<String, String> propertiesMap;
    @Override
    public void processProperties(ConfigurableListableBeanFactory beanFactory, Properties properties) {
        try {
            String config = PropertiesUtil.class.getResource("/").getPath()+"config/config.properties";
            logger.info("PropertiesUtil loadProperties:{}", config);

            PropertiesLoaderUtils.fillProperties(properties, new FileSystemResource(config));

            propertiesMap = new HashMap<String, String>();
            for (Object obj : properties.keySet()) {
                String key = obj.toString();
                String value = properties.getProperty(key);

                properties.put(key, value);
                propertiesMap.put(key, value);
            }
        } catch (Exception e) {
            logger.error("PropertiesUtil loadProperties error:", e);
        }

        super.processProperties(beanFactory, properties);
    }

    public static String getProperty(String key) {
        return propertiesMap.get(key);
    }

}
